<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    h1 {
      text-align: center;
    }

    .add-box {
      user-select: none;
    }

    .level {
      margin: 10px;
    }

    .selectlevel {
      color: red;
    }

    .list {
      margin-top: 20px;
      border: 1px solid #333;
    }
    .item {
      display: flex;
      justify-content: space-between;
    }

    .restore {
      color: green;
    }
  </style>
</head>
<body>

  <div id="app">
    <h1>bug管理系统</h1>
    <div class="add-box">
      <span>产品：</span>
      <input type="text" v-model="goodsName">
      
      <br>
      <span>bug描述:</span>
      <input type="text" v-model="bugInfo">
      
      <br>
      <span>优先级：</span>
      <span v-for="(item,idx) in level" class="level" :class="item == selectLevel ? 'selectlevel': ''" @click="selectLevel = item" :key="idx">{{item}}</span>

      <br>
      <button @click="submitBug()">提交</button>

      <div class="list">
        <div class="item" :class="item.restore ? 'restore' : ''" v-for="(item,idx) in bugList" :key="idx">
          <span>{{item.goodsName}}</span>
          <span>{{item.bugInfo}}</span>
          <span>{{item.level}}</span>
          <button :disabled="item.restore" @click="restoreBug(item)">已修复</button>
          <button @click="removeBug(item._id)">删除</button>
        </div>
      </div>
    </div>
  </div>
  
  <script src="./vue.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    var vm = new Vue({
      el: '#app',
      
      data: {
        level: ['高','中','低'],
        selectLevel: '',
        goodsName: '',
        bugInfo: '',
        bugList: []
      },
      
      created() {
        this.fetchBugList()
      },

      methods: {
        submitBug() {
          if (this.selectLevel == '' || this.goodsName == '' || this.bugInfo == '') {
            alert('请填写完整的BUG信息')
            return false
          }

          axios.post('/addBug',{
            goodsName: this.goodsName,
            bugInfo: this.bugInfo,
            level: this.selectLevel
          }).then(res => {
            if(res.data.code == 1) {
              this.bugList.push(res.data.data)
            }
          })
        },

        fetchBugList() {
          axios.get('/bugList').then(res => {
            this.bugList = res.data.data
          })
        },

        removeBug(id) {
          axios.post('/deleteBug',{_id: id}).then(res => {
            this.bugList.forEach((item,idx) => {
              if (item._id == id) {
                this.bugList.splice(idx,1);
              }
            })
          })
        },

        restoreBug(item) {
          axios.post('/updateBug',{_id: item._id}).then(res => {
            // if(res.data.data.nModified == 1) {
              item.restore = true
            // }
          })
        }
      }

    })
  </script>
</body>
</html>